<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <!-- 导入vue -->
    <script src="https://cdn.bootcdn.net/ajax/libs/vue/2.6.9/vue.js"></script>
    <!-- 导入axios -->
    <script src="https://cdn.bootcdn.net/ajax/libs/axios/0.21.0/axios.js"></script>
    <!-- 引入样式 -->
    <link rel="stylesheet" href="https://unpkg.com/element-ui/lib/theme-chalk/index.css">
    <!-- 引入组件库 -->
    <script src="https://unpkg.com/element-ui/lib/index.js"></script>
</head>
<body>
    <div id="app">
        <el-button @click='login'>登录</el-button>
        <el-button @click='findAll'>查询文章</el-button>
        <ul>
            <li v-for='item,index in article'>
                <h1 @click='detail(item.id)'>{{item.title}} {{item.id}}</h1>
                <div v-html='item.content'></div>
            </li>
        </ul>

        <el-dialog
            title="提示"
            :visible.sync="dialogVisible"
            width="60%">
            <div>{{article_detail.title}}</div>
            <div v-html='article_detail.content'></div>
            <span slot="footer" class="dialog-footer">
                <el-button @click="dialogVisible = false">取 消</el-button>
                <el-button type="primary" @click="dialogVisible = false">确 定</el-button>
            </span>
        </el-dialog>
    </div>
    <script>
        // 添加请求拦截器
        axios.interceptors.request.use(function (config) {
            // 在发送请求之前做些什么
            config.headers['Authorization'] = localStorage.getItem('token');
            return config;
        }, function (error) {
            // 对请求错误做些什么
            return Promise.reject(error);
        });

        // 添加响应拦截器
        axios.interceptors.response.use(function (response) {
            // 对响应数据做点什么
            var res = response.data;
            return res;
        }, function (error) {
            // 对响应错误做点什么
            return Promise.reject(error);
        });
        
        new Vue({
            el:'#app',
            data(){
                return {
                    article:[],
                    dialogVisible:false,
                    article_detail:{}
                }
            },
            methods: {
                detail(id) {
                    this.dialogVisible = true;
                    var obj = {
                        id:id
                    }
                    axios({
                        url:'http://47.93.255.92:8001/article/findById',
                        method:'get',
                        params:obj
                    }).then((res)=>{
                        this.article_detail = res.data;
                    })
                },
                // detail(id) {
                //     this.dialogVisible = true;
                //     axios({
                //         url:'http://47.93.255.92:8001/article/findById',
                //         method:'get',
                //         params:{id}
                //     }).then((res)=>{
                //         console.log(res);
                //     })
                // },

                login() {
                    // 1. 封装参数
                    var obj = {
                        username:'admin',
                        password:123321
                    }
                    // 2. 发送网络请求
                    axios({
                        url:'http://47.93.255.92:8001/user/login',
                        method:'post',
                        data:obj
                    }).then((res)=>{
                        console.log(res.data.token);
                        localStorage.setItem('token',res.data.token);
                    })
                },
                findAll() {
                    axios({
                        url:'http://47.93.255.92:8001/article/findAll',
                        method: 'get'
                    }).then((res)=>{
                        this.article = res.data;
                    })
                }
            }
        })
    </script>
</body>
</html>